package com.sun.commons.utils;

import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.EntityPathBase;

public class QueryDslOrderUtil {

    /**
     * 根据入参动态排序
     * 使用示例： jpaQuery.orderBy(QueryDslOrderUtil.getSortedColumn(qSysUserEntity,params.getSortType(),params.getSortField
     * ()));
     *
     * @param pathBase  q实体
     * @param isAsc  排序类型，升序ASC，降序DESC
     * @param sortField 排序字段名
     * @param <T>
     * @return
     */
    public static <T> OrderSpecifier<?> orderByField(EntityPathBase<T> pathBase, String sortField, boolean isAsc) {
        /*if (StringUtils.isAnyBlank(sortType, sortField)) {
            sortType = "desc";
            sortField = "createTime";
        }*/
        Order order = isAsc ? Order.ASC : Order.DESC;
        com.querydsl.core.types.Path<Object> fieldPath = com.querydsl.core.types.dsl.Expressions.path(Object.class,
                pathBase, sortField);
        return new OrderSpecifier(order, fieldPath);
    }
}
